import React from 'react';
import { inject, observer } from 'mobx-react';
import {WeaTableNew} from 'comsMobx';
const WeaTable = WeaTableNew.WeaTable;

import { WeaErrorPage, WeaTools } from 'ecCom';
import {WeaRightMenu,WeaTop,WeaAuth,WeaDialog,WeaTab,WeaButtonIcon} from 'ecCom';
import {Modal, Button,Icon,Row,Col} from 'antd';
const confirm = Modal.confirm;
import {routerShape} from 'react-router';
import { WeaLocaleProvider } from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;
import * as mobx from 'mobx'

/**
 * 共享模态框
 */
@inject('shareModalStore')
@observer
class ShareModal extends React.Component{

	static contextTypes = {
	    router: routerShape
	};

	constructor(props){
		super(props);
		
	}

	componentDidMount(){	
		//一些初始化请求
		const { shareModalStore } = this.props;		
		const {setRouter} = shareModalStore;
		let {router} = this.context;
        setRouter(router);		
	}

	componentWillReceiveProps(nextProps) {
		//console.log("WillReceive_this.props", this.props);
		//console.log("WillReceive_nextProps", nextProps);	
	}

	shouldComponentUpdate(nextProps, nextState) {
		//console.log("shouldComponent_this.props", this.props);
		//console.log("shouldComponent_nextProps", nextProps);	
		if(nextProps.showShareModel == false && nextProps.showShareModel == this.props.showShareModel){
			return false;
		}
		return true;
	}

	componentWillUnmount() {
		//组件卸载时一般清理一些状态
	}

	

	render(){
		const { shareModalStore,id,title,rptTypeName,showShareModel,tableStore } = this.props;
		const {modalTitle,loading,showAute,setShowAute,condition,setGroupGuid1} = shareModalStore;

		return (

			<div >
				
				<WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@8io695`}
					className = "weaver-fna-shareModal"
					icon='icon-coms-Finance'
              		iconBgcolor='#f14a2d'
					title={getLabel(2112,'共享设置')}
					visible={showShareModel}
					style={{width:830}}
					maskClosable={false}
					onCancel={this.closeShareModal}
					buttons={this.getBottomButton()}	
					moreBtn={{datas:this.getRightMenu(),collectParams:{favname:getLabel(28111,'收藏'), favouritetype:5}}}	
				>
				<WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@lz2gie`} datas={this.getRightMenu()} collectParams={{favname:getLabel(28111,'收藏'), favouritetype:5}}>
				<WeaTab ecId={`${this && this.props && this.props.ecId || ''}_WeaTab@7368s4`}
					buttons={[
					          <WeaButtonIcon ecId={`${this && this.props && this.props.ecId || ''}_WeaButtonIcon@k5omz6@ShowAuteInfo`} buttonType='add' type="primary" onClick={this.showAuteInfo.bind(this)}/>,
					          <WeaButtonIcon ecId={`${this && this.props && this.props.ecId || ''}_WeaButtonIcon@hq9dqk@BatchDelete`} buttonType='del' type="primary" onClick={this.batchDelete.bind(this)} disabled={tableStore.selectedRowKeys.length>0?false:true}/>,
					        ]}
                /> 
				<WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@jxiaq7`} 
                    comsWeaTableStore={tableStore}
                	hasOrder={true}
                	needScroll={true} 
                	onOperatesClick={this.operator.bind(this)}
                /> 
                
                <WeaAuth ecId={`${this && this.props && this.props.ecId || ''}_WeaAuth@o5i3h3`}
                	visible={showAute}
                	conditions={mobx.toJS(condition)}
		            title={getLabel(18645,'添加共享')}
		            onOk={this.saveAuteInfo.bind(this)}
		            onCancel={() => {
						setShowAute(false);
						setGroupGuid1('')
		            }}
                />            
				</WeaRightMenu>
				</WeaDialog>
				
			</div>
		)
	}

	closeShareModal = (e) =>{
		this.props.setShowShareModel(false);
	}

	getRightMenu(){
		let btns = [];
		const {rptTypeName,dataKey,tableStore,shareModalStore} = this.props;
		const {setShowAute} = shareModalStore;

		const that = this;

		btns.push({
            key:"addShare",
    		icon: <i className='icon-coms-Preservation'/>,
            content:getLabel(18645,'添加共享'),
            onClick: () =>{
               that.showAuteInfo();
            }
		});

		btns.push({
            key:"deleteShare",
    		icon: <i className='icon-coms-delete'/>,
    		disabled:tableStore.selectedRowKeys.length>0?false:true,
            content:getLabel(18646,'删除共享'),
            onClick: () =>{
                that.batchDelete();
            }
		});

		btns.push({
            key:"colSet",
    		icon: <i className='icon-coms-Custom'/>,
            content:getLabel(32535,'显示列定制'),
            onClick: () =>{
                tableStore.setColSetVisible(dataKey,true);
    		    tableStore.tableColSet(dataKey,true);
            }
    	});

		return btns;

	}

	getBottomButton(){
		const { tableStore } = this.props;
		let btns = [];
        return btns;
	}


	//共享modal的取消
	getModalFooter(){
		let ShareModalfooter=[
        	<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@txkiq7@Close`} key="back" type="ghost" size="large" onClick={this.closeShareModal}>{getLabel(309,'关闭')}</Button>
        ]
        return ShareModalfooter;
	}


	 //操作判断按钮
	operator(record,index,operate,flag){
		const {shareModalStore} = this.props;
		const {rptTypeName,queryShareData,id} = this.props;
		const {deleteShare,addShare,setChildTest,setGroupGuid1} = shareModalStore;
		let groupGuid1 = record.groupGuid1;

		//console.log("record",record);	
		
		if(flag=='0'){
			//删除
			confirm({
				 title: getLabel(131329,'信息确认'),
		         content: getLabel(15097,'确定要删除吗?'),
		         okText: getLabel(826,'确定'),
				 cancelText: getLabel(31129,'取消'),
    			 onOk() {
  				 	deleteShare({'operation':'delFnaRptShare','rptTypeName':rptTypeName,'groupGuid1':groupGuid1});
					queryShareData({'fnaTmpTbLogId':id,'rptTypeName':rptTypeName});
    			 },
    			 onCancel() {}
			});		
			
		}else if(flag=='1'){
			//编辑
			
			setGroupGuid1(groupGuid1);
			this.showAuteInfo();	
		}
	}

	//显示共享录入信息
	showAuteInfo(){
		const {shareModalStore} = this.props;
		const { rptTypeName,id } = this.props;
		const { setShowAute,shareInfo,groupGuid1 } = shareModalStore;
		shareInfo({'fnaTmpTbLogId':id, 'rptTypeName':rptTypeName, 'groupGuid1':groupGuid1});
	}


	//保存录入信息
	saveAuteInfo(params){
		const {shareModalStore} = this.props;
		const {addShare,groupGuid1,setShowAute} = shareModalStore;
		const {rptTypeName,queryShareData,id } = this.props;

		if(params.shareType != 0){
			if(params.shareId == undefined){
				alert(getLabel(386003,"对象必填！"));
				setShowAute(false);
				return;
			}
		}

		let newParams = Object.assign(params,{
			operation:'FnaRptShareSave',
			rptTypeName:rptTypeName,
			fnaTmpTbLogId:id,
			groupGuid1:groupGuid1
		});
		
		//console.log("newParams", newParams);
		addShare(newParams,queryShareData);//传入查询方法,这里存在异步时间问题
	}

	//批量删除
    batchDelete(){
    	const { shareModalStore } = this.props;
    	const { tableStore,rptTypeName,queryShareData,id } = this.props;
    	const { deleteShare } = shareModalStore;
    	const { selectedRowKeys } = tableStore;
    	let ids = "";
    	if(selectedRowKeys.length <= 0){
    		Modal.warning({
			    title: getLabel(15172,'系统提示'),
			    content: getLabel(32568,'请选择要删除的记录!'),
			    okText: getLabel(826,'确定'),
			    onOk() {},
			  });
    	}else{
			confirm({
				 title: getLabel(131329,'信息确认'),
    			 content: getLabel(15097,'确定要删除吗?'),
    			 okText: getLabel(826,'确定'),
				 cancelText: getLabel(31129,'取消'),
    			 onOk() {
    			 	for(let listId of selectedRowKeys){
		    			ids += listId + ",";
		    		}
		    		ids = ids.substring(0,ids.lastIndexOf(","));
		    		let params = Object.assign({},{
						operation:'batchDelFnaRptShare',
						rptTypeName:rptTypeName,
						batchDelIds:ids,
						fnaTmpTbLogId:id
					});
  				 	deleteShare(params,queryShareData);//传入查询方法,这里存在异步时间问题
    			 },
    			 onCancel() {}
			});	
    	}
    }
	

}


export default WeaTools.tryCatch(React,
	props => <WeaErrorPage ecId={`${this && this.props && this.props.ecId || ''}_WeaErrorPage@fyzpbp`} msg={ props.error ? props.error : getLabel(385218,'对不起,该页面异常,请联系管理员!')} />,
	{error: ""}
)(ShareModal);